swadhinbiswas # Changelog · 1/17/2026
1464862
35 lines · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { getStorage } from "@/lib/storage";
import { verifyStorageSignature } from "@/lib/storage-auth";
import type { APIRoute } from "astro";
import { Readable } from "stream";

export const GET: APIRoute = async ({ params, url }) => {
  const { key } = params;
  if (!key) return new Response("Missing key", { status: 400 });

  // Verify signature for secure access
  const signature = url.searchParams.get("sig");
  const expires = url.searchParams.get("exp");

  if (!verifyStorageSignature(key, signature, expires)) {
    return new Response("Unauthorized - Invalid or expired signature", { status: 401 });
  }

  const storage = await getStorage();

  try {
    const stream = await storage.getStream(key);
    const stat = await storage.stat(key);

    return new Response(Readable.toWeb(stream) as any, {
      headers: {
        "Content-Type": stat.contentType || "application/octet-stream",
        "Content-Length": stat.size.toString(),
        "Cache-Control": "private, max-age=3600",
      },
    });
  } catch (e) {
    return new Response("Not Found", { status: 404 });
  }
};